package com.fang.www.wg.crawler;

import java.io.IOException;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.fang.www.wg.util.DisCompetitionColection;
import com.fang.www.wg.util.RegexUtil;
import com.fang.www.wg.util.UrlCollection;
import com.fang.www.wgenum.RegexGroupName;

public class FundationalScratch {
	private CloseableHttpClient httpClient;
	private DisCompetitionColection disCompetitionColection;
	private UrlCollection urlCollection;
	private RegexUtil regexUtil;
	private Logger logger =Logger.getLogger(FundationalScratch.class);
 	public FundationalScratch( DisCompetitionColection disCompetitionColection,
			UrlCollection urlCollection, RegexUtil regexUtil) {
		super();
		this.disCompetitionColection = disCompetitionColection;
		this.urlCollection = urlCollection;
		this.regexUtil = regexUtil;
	}
 	public FundationalScratch(){
 		
 	}
	private String getPageContent() throws ClientProtocolException, IOException{
		String responseBody = "";
		httpClient = HttpClients.createDefault();
		try {
			RequestConfig requestConfig = RequestConfig.custom()  
	                .setConnectTimeout(5000).setConnectionRequestTimeout(5000)  
	                .setSocketTimeout(5000).build();  
			String url  = urlCollection.getUrl();
			logger.info("正在爬虫=========="+url);
			HttpGet httpget = new HttpGet(url);
			httpget.setConfig(requestConfig);
			logger.info("Executing request " + httpget.getRequestLine());
			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

				public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
					// TODO Auto-generated method stub
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toString(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}
			};
			disCompetitionColection.addDisCompetitionColection(url);
			responseBody = httpClient.execute(httpget, responseHandler);
			logger.info("---------------------------------------------");
			logger.info(responseBody);
		} catch(Exception e){
			logger.info("错了");
		}finally {
			// TODO: handle finally clause
			httpClient.close();
		}
		return responseBody;
	}
	public void getUrlAndValiableContent() throws ClientProtocolException, IOException{
		String pageContent = getPageContent();
		if(pageContent.length()!=0){
			RegexUtil regexUtil =new RegexUtil("\\<a href=(\"(?<"+RegexGroupName.URL+">http://news.163.com.+?)\")+?.+\\>.+?\\</a\\>", pageContent);
			Set<String> set = regexUtil.getTheMatcherContent();
			for(String url:set){
				if(!disCompetitionColection.ifDispetion(url)){
					urlCollection.addCollects(url);
				}
			}
			disCompetitionColection.printCompetionColletion();
		}
		
		
	}
	public static void main(String[] args)  {
		DisCompetitionColection disCompetitionColection = new DisCompetitionColection();
		UrlCollection urlCollection = new UrlCollection();
		urlCollection.addCollects("http://news.163.com/");
		FundationalScratch fundationalScratch = new FundationalScratch(disCompetitionColection,urlCollection,null);
		while(true){
			try {
				Thread.sleep(5000);
				fundationalScratch.getUrlAndValiableContent();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
